package com.wsd.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.struts2.json.annotations.JSON;

/**
 * Products entity. @author MyEclipse Persistence Tools
 */

public class Products implements java.io.Serializable {

	// Fields

	private Integer PId;
	private Category category;
	private String PName;
	private String model;
	private String barcode;
	private String description;
	private String image;
	private String factory;
	private Set orderLists = new HashSet(0);
	private Set productRanks = new HashSet(0);
	private Set prices = new HashSet(0);
	private Set productComments = new HashSet(0);

	// Constructors

	/** default constructor */
	public Products() {
	}

	/** minimal constructor */
	public Products(String PName, String model, String barcode,
			String description, String image, String factory) {
		this.PName = PName;
		this.model = model;
		this.barcode = barcode;
		this.description = description;
		this.image = image;
		this.factory = factory;
	}

	/** full constructor */
	public Products(Category category, String PName, String model,
			String barcode, String description, String image, String factory,
			Set orderLists, Set productRanks, Set prices, Set productComments) {
		this.category = category;
		this.PName = PName;
		this.model = model;
		this.barcode = barcode;
		this.description = description;
		this.image = image;
		this.factory = factory;
		this.orderLists = orderLists;
		this.productRanks = productRanks;
		this.prices = prices;
		this.productComments = productComments;
	}

	// Property accessors

	public Integer getPId() {
		return this.PId;
	}

	public void setPId(Integer PId) {
		this.PId = PId;
	}
	
	public Category getCategory() {
		return this.category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public String getPName() {
		return this.PName;
	}

	public void setPName(String PName) {
		this.PName = PName;
	}

	public String getModel() {
		return this.model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getBarcode() {
		return this.barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getImage() {
		return this.image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getFactory() {
		return this.factory;
	}

	public void setFactory(String factory) {
		this.factory = factory;
	}
	@JSON(serialize = false)
	public Set getOrderLists() {
		return this.orderLists;
	}

	public void setOrderLists(Set orderLists) {
		this.orderLists = orderLists;
	}
	@JSON(serialize = false)
	public Set getProductRanks() {
		return this.productRanks;
	}

	public void setProductRanks(Set productRanks) {
		this.productRanks = productRanks;
	}
	@JSON(serialize = false)
	public Set getPrices() {
		return this.prices;
	}

	public void setPrices(Set prices) {
		this.prices = prices;
	}
	
	public String getShowPrice(){
		List<Price> prices = new ArrayList<Price>(getPrices());
		Collections.sort(prices,new Comparator<Price>(){
			public int compare(Price o1, Price o2) {
				return o1.getULevel() - o2.getULevel();
			}
		});
		StringBuilder sb = new StringBuilder();
		int len = prices.size();
		for(int i = 0; i < len; i++){
			Price price = prices.get(i);
			sb.append(price.getPrice());
			if(i + 1 < len){
				sb.append("/");
			}
		}
		
		return sb.toString();
	}
	
	public Map<Integer,Float> getPriceMap(){
		Map<Integer,Float> priceMap = new HashMap<Integer, Float>();
		for (Iterator iter = getPrices().iterator(); iter.hasNext();) {
			Price price = (Price) iter.next();
			priceMap.put(price.getULevel(), price.getPrice());
			
		}
		return priceMap;
	}

	/**
	 * @param productComments the productComments to set
	 */
	public void setProductComments(Set productComments) {
		this.productComments = productComments;
	}

	/**
	 * @return the productComments
	 */
	@JSON(serialize = false)
	public Set getProductComments() {
		return productComments;
	}
	public int getProductCommentsCount(){
		return getProductComments().size();
	}
}